import { API_ENDPOINTS } from "../api/config";
import { BaseService } from "../base/requests";
import {
  LLMModelCreateRequest,
  LLMModelUpdateRequest,
  LLMModelResponse,
  LLMModelListResponse,
  LLMModelFilterRequest,
  LLMModelPaginationRequest,
  MessageResponse
} from "../types/generated/llm";
import type { ApiResponse } from "../types";

/**
 * LLM模型管理服务类
 * 处理LLM模型的增删改查操作
 * 实现单例模式，确保全局只有一个服务实例
 * @extends {BaseService}
 */
class LLMCreateService extends BaseService {
  private static instance: LLMCreateService;

  /**
   * 私有构造函数，初始化LLM模型管理服务
   * @private
   */
  private constructor() {
    super();
  }

  /**
   * 获取 LLMCreateService 的单例实例
   * @returns {LLMCreateService} LLMCreateService 实例
   * @static
   */
  static getInstance(): LLMCreateService {
    if (!LLMCreateService.instance) {
      LLMCreateService.instance = new LLMCreateService();
    }
    return LLMCreateService.instance;
  }

  /**
   * 创建新的LLM模型
   * @param {LLMModelCreateRequest} data - 模型创建请求数据
   * @returns {Promise<ApiResponse<LLMModelResponse>>} API响应，包含创建的模型信息
   */
  async createModel(data: LLMModelCreateRequest): Promise<ApiResponse<LLMModelResponse>> {
    return this.post<LLMModelResponse>(
      API_ENDPOINTS.llmCreate.create,
      data,
      { withAuth: true }
    );
  }

  /**
   * 获取LLM模型列表
   * @param {LLMModelPaginationRequest} pagination - 分页参数
   * @param {LLMModelFilterRequest} filter - 筛选条件
   * @returns {Promise<ApiResponse<LLMModelListResponse>>} API响应，包含模型列表及分页信息
   */
  async listModels(
    pagination: LLMModelPaginationRequest = {},
    filter: LLMModelFilterRequest = {}
  ): Promise<ApiResponse<LLMModelListResponse>> {
    // 构建查询参数
    const queryParams = new URLSearchParams();
    
    // 添加分页参数
    if (pagination.skip !== undefined) queryParams.append('skip', pagination.skip.toString());
    if (pagination.limit !== undefined) queryParams.append('limit', pagination.limit.toString());
    if (pagination.sort_by) queryParams.append('sort_by', pagination.sort_by);
    if (pagination.sort_order) queryParams.append('sort_order', pagination.sort_order);
    
    // 添加筛选参数
    if (filter.provider) queryParams.append('provider', filter.provider);
    if (filter.modality) queryParams.append('modality', filter.modality);
    if (filter.status) queryParams.append('status', filter.status);
    if (filter.search) queryParams.append('search', filter.search);
    
    // 构建URL
    const url = `${API_ENDPOINTS.llmCreate.listModels}?${queryParams.toString()}`;
    
    return this.get<LLMModelListResponse>(url, { withAuth: true });
  }

  /**
   * 获取指定ID的LLM模型
   * @param {string} modelId - 模型ID
   * @returns {Promise<ApiResponse<LLMModelResponse>>} API响应，包含模型信息
   */
  async getModel(modelId: string): Promise<ApiResponse<LLMModelResponse>> {
    return this.get<LLMModelResponse>(
      `${API_ENDPOINTS.llmCreate.getModel.replace('{model_id}', modelId)}`,
      { withAuth: true }
    );
  }

  /**
   * 更新指定ID的LLM模型
   * @param {string} modelId - 模型ID
   * @param {LLMModelUpdateRequest} data - 模型更新请求数据
   * @returns {Promise<ApiResponse<LLMModelResponse>>} API响应，包含更新后的模型信息
   */
  async updateModel(
    modelId: string,
    data: LLMModelUpdateRequest
  ): Promise<ApiResponse<LLMModelResponse>> {
    return this.put<LLMModelResponse>(
      `${API_ENDPOINTS.llmCreate.updateModel.replace('{model_id}', modelId)}`,
      data,
      { withAuth: true }
    );
  }

  /**
   * 删除指定ID的LLM模型
   * @param {string} modelId - 模型ID
   * @returns {Promise<ApiResponse<MessageResponse>>} API响应，包含删除成功的消息
   */
  async deleteModel(modelId: string): Promise<ApiResponse<MessageResponse>> {
    return this.delete<MessageResponse>(
      `${API_ENDPOINTS.llmCreate.deleteModel.replace('{model_id}', modelId)}`,
      { withAuth: true }
    );
  }
}

/** 导出 LLMCreateService 单例实例 */
export const llmCreateService = LLMCreateService.getInstance();
